
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;
using System.Collections.Generic;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.BLL.Enums;
using Tathata.Web.Administration.Modules;

namespace Tathata.Web.Administration
{
    public partial class Administration_ProductVariantAttributeValues : BaseNopAdministrationPage
    {
        private void BindData()
        {
            ProductVariantAttribute productVariantAttribute = ProductVariantAttributeManager.GetByProductVariantAttributeID(this.ProductVariantAttributeID);
            if (productVariantAttribute != null)
            {
                ProductVariant productVariant = productVariantAttribute.ProductVariant;
                if (productVariant == null)
                    Response.Redirect("Products.aspx");
                ProductAttribute productAttribute = productVariantAttribute.ProductAttribute;
                if (productAttribute == null)
                    Response.Redirect("Products.aspx");

                lblTitle.Text = string.Format("Adding/Editing values for [{0}] attribute. <a href=\"ProductVariantDetails.aspx?ProductVariantID={1}\">{2}</a>", productAttribute.Name, productVariant.ProductVariantID, productVariant.FullProductName);
               
                ProductVariantAttributeValueCollection productVariantAttributeValues = productVariantAttribute.ProductVariantAttributeValues;
                if (productVariantAttributeValues.Count > 0)
                {
                    gvProductVariantAttributeValues.Visible = true;
                    gvProductVariantAttributeValues.DataSource = productVariantAttributeValues;
                    gvProductVariantAttributeValues.DataBind();
                }
                else
                    gvProductVariantAttributeValues.Visible = false;
            }
            else
                Response.Redirect("Products.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gvProductVariantAttributeValues.Columns[1].HeaderText = string.Format("Price Adjustment [{0}]", CurrencyManager.PrimaryStoreCurrency.CurrencyCode);
               
                this.BindData();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                ProductVariantAttribute productVariantAttribute = ProductVariantAttributeManager.GetByProductVariantAttributeID(this.ProductVariantAttributeID);
                if (productVariantAttribute != null)
                {
                    ProductVariantAttributeValue productVariantAttributeValue = ProductVariantAttributeValueManager.InsertProductVariantAttributeValue(productVariantAttribute.ProductVariantAttributeID,
                        txtNewName.Text, txtNewPriceAdjustment.Value, txtNewDisplayOrder.Value);

                    BindData();

                    txtNewName.Text = string.Empty;
                    txtNewPriceAdjustment.Value = 0;
                    txtNewDisplayOrder.Value = 1;
                }
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void gvProductVariantAttributeValues_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateProductVariantAttributeValue")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvProductVariantAttributeValues.Rows[index];

                HiddenField hfProductVariantAttributeValueID = row.FindControl("hfProductVariantAttributeValueID") as HiddenField;
                SimpleTextBox txtName = row.FindControl("txtName") as SimpleTextBox;
                DecimalTextBox txtPriceAdjustment = row.FindControl("txtPriceAdjustment") as DecimalTextBox;
                NumericTextBox txtDisplayOrder = row.FindControl("txtDisplayOrder") as NumericTextBox;

                int productVariantAttributeValueID = int.Parse(hfProductVariantAttributeValueID.Value);
                string name = txtName.Text;
                decimal priceAdjustment = txtPriceAdjustment.Value;
                int displayOrder = txtDisplayOrder.Value;

                ProductVariantAttributeValue productVariantAttributeValue = ProductVariantAttributeValueManager.GetByProductVariantAttributeValueID(productVariantAttributeValueID);

                if (productVariantAttributeValue != null)
                    ProductVariantAttributeValueManager.UpdateProductVariantAttributeValue(productVariantAttributeValue.ProductVariantAttributeValueID,
                       productVariantAttributeValue.ProductVariantAttributeID, name,
                       priceAdjustment, displayOrder);

                BindData();
            }
        }

        protected void gvProductVariantAttributeValues_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ProductVariantAttributeValue productVariantAttributeValue = (ProductVariantAttributeValue)e.Row.DataItem;

                Button btnUpdate = e.Row.FindControl("btnUpdate") as Button;
                if (btnUpdate != null)
                    btnUpdate.CommandArgument = e.Row.RowIndex.ToString();
            }
        }

        protected void gvProductVariantAttributeValues_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productVariantAttributeValueID = (int)gvProductVariantAttributeValues.DataKeys[e.RowIndex]["ProductVariantAttributeValueID"];
            ProductVariantAttributeValueManager.DeleteProductVariantAttributeValue(productVariantAttributeValueID);
            BindData();
        }

        public int ProductVariantAttributeID
        {
            get
            {
                return CommonHelper.QueryStringInt("ProductVariantAttributeID");
            }
        }
    }
}